/**
 * 收集系统信息
 */
import { getLogger, Service } from '@gnlab/utils'
import { BehaviorSubject } from 'rxjs'
import config from '@/config'
import { RxStream } from '@/lib/utils'
import type { Services } from '@/services'
import { API } from '@/services/common/api'

const logger = getLogger('services/app-info')

export class AppInfo extends Service<Services> {
  get auth() {
    return this.services.auth
  }

  registered() {
    void this.auth.accountId.then(() => {
      this.detectVersions()
    })
  }

  private readonly versions$$ = new BehaviorSubject({
    ui: config.APP_VERSION,
    api: '',
  })
  readonly versions$ = new RxStream(this.versions$$)

  protected detectVersions() {
    // 确认接口版本
    void API<string>('/Config/Version').then(res => {
      if (res.success) {
        this.versions$.next({
          ...this.versions$.value,
          api: res.data,
        })
        logger.info(`API 版本：${res.data}`)
      } else {
        logger.error('获取 API 版本失败', res.error)
      }
    })
  }
}
